// pages/my/my.js
Page({
  data: {
    list: [],
    name: '',
    price: 0
  },
  onShow() {
    this.getList()
  },
  onLoad() {
    this.getList()
  },
  getList() {
    wx.cloud.database().collection('list')
      .get()
      .then(res => { //查询成功后的响应函数
        this.setData({
          list: res.data
        })
      })
      .catch(err => { //查询失败后的响应函数
        console.log(err)
      })
  },
  getName(e) {
    this.setData({
      name: e.detail.value
    })
  },
  getPrice(e) {
    this.setData({
      price: Number.parseInt(e.detail.value)
    })
  },
  add() {
    wx.cloud.database().collection('list')
      .add({
        data: {
          name: this.data.name,
          price: this.data.price
        }
      }).then(res => {
        this.getList()
        console.log(res)
      }).catch(err => {
        console.log(err)
      })
  },
  getDetail(e) {
    wx.navigateTo({
      url: '/pages/detail/detail?id=' + e.target.dataset.id,
    })
  }
})